package com.ctshk.rpc.localfun.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.rpc.localfun.dto.OperationLogListDTO;
import com.ctshk.rpc.localfun.entity.OperationLog;
import com.ctshk.rpc.localfun.mapper.OperationLogMapper;
import com.ctshk.rpc.localfun.req.OperationLogListReq;
import com.ctshk.rpc.localfun.service.IOperationLogService;
import com.ctshk.rpc.system.dto.UserDTO;
import com.ctshk.rpc.system.service.ISysUserService;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 操作日志表 服务实现类
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-02-24
 */
@DubboService
public class OperationLogServiceImpl extends ServiceImpl<OperationLogMapper, OperationLog> implements IOperationLogService {
    @Autowired
    private OperationLogMapper operationLogMapper;

    @DubboReference
    private ISysUserService iSysUserService;

    public PageResponse<OperationLogListDTO> list(OperationLogListReq operationLogListReq) {
        IPage<OperationLog> operationLogIPage = null;
        QueryWrapper<OperationLog> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(operationLogListReq.getUserId())) {
            queryWrapper.lambda().eq(OperationLog::getUserId, operationLogListReq.getUserId());
        }
        if (StringUtils.isNotBlank(operationLogListReq.getGmtCreateStart())) {
            queryWrapper.lambda().ge(OperationLog::getGmtCreate, operationLogListReq.getGmtCreateStart());
        }
        if (StringUtils.isNotBlank(operationLogListReq.getGmtCreateEnd())) {
            queryWrapper.lambda().le(OperationLog::getGmtCreate, operationLogListReq.getGmtCreateEnd());
        }
        Page<OperationLog> iPage = new Page<>(operationLogListReq.getPageNo(), operationLogListReq.getPageSize());
        operationLogIPage = operationLogMapper.selectPage(iPage, queryWrapper);
        if (CollectionUtils.isEmpty(operationLogIPage.getRecords())) {
            return PageResponse.empty(operationLogIPage.getCurrent());
        }
        List<OperationLogListDTO> operationLogListDTOS = new ArrayList<>();
        operationLogIPage.getRecords().forEach(operationLog -> {
            OperationLogListDTO operationLogListDTO = EntityUtil.copy(operationLog, OperationLogListDTO.class);
            if(operationLog.getUserId() != null){
                UserDTO userDTO = iSysUserService.loadUserByParam(operationLog.getUserId(), 1);
                operationLogListDTO.setCreateUser(userDTO.getUsername()+"/"+userDTO.getDept());
            }
            operationLogListDTOS.add(operationLogListDTO);
        });
        PageResponse<OperationLogListDTO> result = new PageResponse<>(operationLogListDTOS, operationLogIPage.getCurrent() == 1, operationLogIPage.getTotal() <= (operationLogIPage.getCurrent() * operationLogIPage.getSize()), operationLogIPage.getTotal(), operationLogIPage.getCurrent(), operationLogIPage.getSize());
        return result;
    }
}
